package com.orangelife.service.propinfo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propinfo.IPropInfoService;
import com.orangelife.service.propinfo.model.KlCmmApartments;
import com.orangelife.service.propinfo.model.KlCmmBuilding;
import com.orangelife.service.propinfo.model.KlCmmUnit;
import com.orangelife.service.propinfo.model.PropFeeApartment;
import com.orangelife.service.propinfo.model.PropFeeBuilding;
import com.orangelife.service.propinfo.model.PropFeeUnit;
import com.orangelife.service.propinfo.model.QueryModel;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.utils.StringUtils;

@Service("propInfoService")
public class PropInfoServiceImpl extends MybaitsGenericDaoSubLogic implements
		IPropInfoService {

	@Override
	public List<KlCmmBuilding> selectBuildingsByCommId(String commId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commId", commId);
		return this.getSqlSession().selectList(
				"KlCmmBuildingMapper.selectByCommId", map);
	}

	@Override
	public List<KlCmmUnit> selectUnitsByBuildingIdList(List<String> buildids) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", buildids);
		return this.getSqlSession().selectList(
				"KlCmmUnitMapper.selectByBuildingIdList", map);
	}

	@Override
	public List<KlCmmApartments> selectApartmentsByBuildingIdList(
			List<String> buildids) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", buildids);
		return this.getSqlSession().selectList(
				"KlCmmApartmentsMapper.selectByBuildingIdList", map);
	}

	@Override
	public void insertBuildingSelective(PropFeeBuilding building) {
		this.getSqlSession().insert("PropFeeBuildingMapper.insertSelective", building);
		
	}

	@Override
	public void insertUnitSelective(PropFeeUnit unit) {
		this.getSqlSession().insert("PropFeeUnitMapper.insertSelective", unit);
		
	}

	@Override
	public void insertApartmentSelective(PropFeeApartment apartment) {
		this.getSqlSession().insert("PropFeeApartmentMapper.insertSelective", apartment);
		
	}

	@Override
	public Map<String, Object> getApartmentInfo(String apartmentID)
			throws AppBizException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("apartmentID", apartmentID);
		// TODO Auto-generated method stub
		return 		this.getSqlSession().selectOne("propInfoMapper.getPropinfo", map);

	}

	@Override
	public List<Map<String, Object>> getAllInfo(String commID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		
		
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList("propInfoMapper.getAllInfo",map);
	}

	@Override
	public List<Map<String, Object>> getApartmentInfo(QueryModel queryModel)
			throws AppBizException {
		
	
	//	String type="-1";
				
		
		// TODO Auto-generated method stub
	return 		this.getSqlSession().selectList("propInfoMapper.getPropinfo", queryModel);

	}

	@Override
	public List<Map<String, Object>> getBuilding(String commID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		
		
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList("propInfoMapper.getBuildingBycommID",map);
	
	}

	@Override
	public List<Map<String, Object>> getUnits(String buildingID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buildingID", buildingID);
		
		
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList("propInfoMapper.getUnitBybuildingID",map);
	}

	@Override
	public List<Map<String, Object>> getApartmentsByBuilding(String buildingID,
			String unitID) throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(StringUtils.isBlank(buildingID)&&StringUtils.isBlank(unitID)){
			
			throw new AppBizException(CodeConst.SYS_CODE_1001, "楼栋ID和单元ID不能同时为空");
		}
	
		map.put("buildingID", buildingID);
		map.put("unitID", unitID);
		return this.getSqlSession().selectList("propInfoMapper.getApartmentsByBuildingIDAndUnitID",map);
		

	}

	@Override
	public List<Map<String, Object>> getApartments(Map<String, Object> param)
			throws AppBizException {
		
		int pageNo=1;
		int pageSize=10;
		if(param.get("pageIndex")!=null){
			
			pageNo=Integer.parseInt(param.get("pageIndex").toString());
			
		}
          if(param.get("pageSize")!=null){
			
	             pageSize=Integer.parseInt(param.get("pageSize").toString());
	         		
	    	}
	
          PageHelper.startPage(pageNo, pageSize);
		
	
		return this.getSqlSession().selectList("propInfoMapper.getApartments",param);
	}

	@Override
	public List<Map<String, Object>> getOwners(Map<String, Object> param)
			throws AppBizException {
		// TODO Auto-generated method stub
		
		
		int pageNo=1;
		int pageSize=10;
		if(param.get("pageIndex")!=null){
			
			pageNo=Integer.parseInt(param.get("pageIndex").toString());
			
		}
          if(param.get("pageSize")!=null){
			
	             pageSize=Integer.parseInt(param.get("pageSize").toString());
	         		
	    	}
	
          PageHelper.startPage(pageNo, pageSize);
		
		return this.getSqlSession().selectList("propInfoMapper.getOwners",param);
	}

}
